package org.walakulu.client.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.security.KeyFactory;
import java.security.KeyStore;
import java.security.PrivateKey;
import java.security.cert.CertificateFactory;
import java.security.spec.PKCS8EncodedKeySpec;


/**
 * @author : Eran Chinthaka (echintha@cs.indiana.edu)
 * @date : Feb 9, 2009 - 10:36:22 PM
 */
public class KeyStoreImporter {
    public static void importToStore(String keyStoreFileName, String privateKeyFileName,
                                     String certificateChainFileName, String entryAlias,
                                     String keyStorePassword) {
        try {

// Load the keystore
            KeyStore keyStore = KeyStore.getInstance("jks");
            keyStore.load(null, keyStorePassword.toCharArray());
            FileInputStream keyStoreInputStream =
                    new FileInputStream(keyStoreFileName);
            keyStore.load(keyStoreInputStream, keyStorePassword.toCharArray());
            keyStoreInputStream.close();

// Load the certificate chain (in X.509 DER encoding).
            FileInputStream certificateStream =
                   new FileInputStream(certificateChainFileName);
            CertificateFactory certificateFactory =
                    CertificateFactory.getInstance("X.509");
// Required because Java is STUPID.  You can't just cast the result
// of toArray to Certificate[].
            java.security.cert.Certificate[] chain = {};
            chain = certificateFactory.generateCertificates(certificateStream).toArray(chain);
            certificateStream.close();

// Load the private key (in PKCS#8 DER encoding).
            File keyFile = new File(privateKeyFileName);
            byte[] encodedKey = new byte[(int) keyFile.length()];
            FileInputStream keyInputStream = new FileInputStream(keyFile);
            keyInputStream.read(encodedKey);
            keyInputStream.close();
            KeyFactory rSAKeyFactory = KeyFactory.getInstance("RSA");
            PrivateKey privateKey = rSAKeyFactory.generatePrivate(
                    new PKCS8EncodedKeySpec(encodedKey));

// Add the new entry
            keyStore.setEntry(entryAlias,
                    new KeyStore.PrivateKeyEntry(privateKey, chain),
                    new KeyStore.PasswordProtection(keyStorePassword.toCharArray())
            );

// Write out the keystore
            FileOutputStream keyStoreOutputStream =
                    new FileOutputStream(keyStoreFileName);
            keyStore.store(keyStoreOutputStream, keyStorePassword.toCharArray());
            keyStoreOutputStream.close();
        }

        catch (Exception e) {
            e.printStackTrace();
            System.exit(1);
        }
    }

}
